package com.kun.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class WebController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/sayHello")
    @HystrixCommand(fallbackMethod = "helloError") //熔断器
    public String sayHello(@RequestParam String name) {

        return restTemplate.getForObject("http://SERVICE-HELLO/hello?name=" + name, String.class);
    }


    /**
     * 当服务不可用时，调用对应的API接口，会执行快速失败，
     * 直接返回一组字符串，而不是等待响应超时，这很好的控制了容器的线程阻塞。
     * */
    public String helloError(String name) {
        return "hi," + name + ",sorry,error!";
    }
}
